
$(function(){
	initValidate();//初始化表单校验
	
});	
 
/**
 * 本模块业务校验：验证角色名称是否已经存在
 */
jQuery.validator.methods["checkIsExists"]=(function(value,element){
	
	//验证是否重复，pass为false代表重复，校验不通过
	var pass = false;
	var roleName = $("#roleName").val();
	var roleId = $("#roleId").val();
	if(roleId == null && roleId == ''){
		roleId = null;
	}
	if(!roleName)
		return true;
	
	$.ajax({
		  url: PATH_PREFIX+"checkRepeatRole.do",
		  dataType: "json",
		  type: "POST",
		  cache: false,
		  async: false,
		  data: {
			  roleName : roleName,
			  roleId : roleId
		  },
		  success: function( data ) {
				if(data.object == true){
					pass = false;
					return;
				}
				pass=true;
		  },
		  error: function(jqXHR, textStatus, errorThrown){
				ctools.alert("查询失败" + textStatus);
				pass = false;
		  }
	});
	return this.optional(element) || pass;
});

function initValidate(){
	 $("#roleAddForm").validate({
	        focusCleanup: true,
	        rules: {
	        	roleName:{
	        		required:true,
	        		checkIsExists:true
	        	},
	        	remark:{
	        		required:false,
	        		maxlength:1000
	        	}
	        },
	        messages: {
	        	roleName:{
	        		required:"请输入角色名称",
	        		checkIsExists:"该操作角色已存在，请重新填写"
	        	},
	        	remark:{
	        		maxlength:"最大长度为1000个文字"
	        	}
	        }
	    });
	
}


function submitAddForm(){	
	SubmitAndPreventSecond("roleAddForm",function(){
		var roleId = $("#roleId").val();
		var path = "saveRole.do";
		if(roleId != null && roleId.length > 0){
			path = "updateRole.do";
		}
		$("#roleAddForm").attr("action",PATH_PREFIX+path);
	});//防止表单二次提交
}		
		
